package com.ttg.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ttg.contants.TestContants;
import com.ttg.contants.UrlContants;
import com.ttg.pojo.BatchPayResultPojo;
import com.ttg.security.EncryptionUtils;
import com.ttg.utils.connection.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/batch_pay_result")
public class BatchPayResultDemoController {

    private static final Logger logger = LoggerFactory.getLogger(BatchPayResultDemoController.class);


    @RequestMapping
    public String testBatchPayResult(Model model){
        BatchPayResultPojo batchPayResultPojo = new BatchPayResultPojo();
        batchPayResultPojo.setPayChannel(TestContants.CP_CHANNEL);
        batchPayResultPojo.setStartDate(new Date());
        batchPayResultPojo.setEndDate(new Date());
        model.addAttribute("batchPayResult",batchPayResultPojo);
        return "demo/batch_pay_result_demo";
    }


    @RequestMapping(value = "/do_batch_pay_result",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject doBatchPayResult(String payChannel, @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate){
        BatchPayResultPojo batchPayResultPojo = new BatchPayResultPojo();
        batchPayResultPojo.setPayChannel(payChannel);
        batchPayResultPojo.setStartDate(startDate);
        batchPayResultPojo.setEndDate(endDate);
        batchPayResultPojo.setMerchantId(TestContants.MERCHANT_ID);
        batchPayResultPojo.setAppId(TestContants.APP_ID);
        batchPayResultPojo.setSignatureAlgorithm(TestContants.SIGNATUREALGORITHM);
        batchPayResultPojo.setSignatureInfo(EncryptionUtils.encryption(batchPayResultPojo.getAllParameters()));
        logger.debug("batch pay result :{}",batchPayResultPojo);
        logger.debug("{}",batchPayResultPojo.getStartDate());
        String resp = HttpClientUtils.sendPostHttpRequest(UrlContants.BATCH_PAY_RESULT_URL, "POST", batchPayResultPojo.getAllParameters());
        return JSON.parseObject(resp);
    }

}
